package com.zhu.controller.user;


import com.zhu.base.http.RestResponse;
import com.zhu.base.util.TokenUtil;
import com.zhu.domain.Areas;
import com.zhu.domain.Cities;
import com.zhu.domain.Provinces;
import com.zhu.domain.User;
import com.zhu.domain.enums.UserRoleEnum;
import com.zhu.model.user.UserRegisterVM;
import com.zhu.model.user.UserResponseVM;
import com.zhu.model.user.UserUpdatePasswordVM;
import com.zhu.model.user.UserUpdateVM;
import com.zhu.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author JoyBoy
 * @since 2022-05-30
 */
//写在类上，不用每个方法都写这个注解了
@CrossOrigin
@RestController
@RequestMapping("/gim/api/user")
@Api(tags = "通用用户模块说明")
public class UserController {
    private final ModelMapper modelMapper;

    private final UserService userService;

    private final InventoryRecordService inventoryRecordService;

    private final AreasService areasService;

    private final CitiesService citiesService;

    private final ProvincesService provincesService;

    @Autowired
    public UserController(ModelMapper modelMapper, UserService userService,InventoryRecordService inventoryRecordService, AreasService areasService, CitiesService citiesService, ProvincesService provincesService) {
        this.modelMapper = modelMapper;
        this.userService = userService;
        this.inventoryRecordService = inventoryRecordService;
        this.areasService = areasService;
        this.citiesService = citiesService;
        this.provincesService = provincesService;
    }

    /**
     * 功能描述 通过token获取用户信息
     *
     * @param authentication
     *
     * @return com.zhu.base.http.RestResponse<java.lang.Object>
     *
     * @author JoyBoyZH
     * @date 2022/5/31
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/current")
    @ApiOperation("获取当前用户信息接口说明")
    public RestResponse<Object> current(@RequestHeader("authentication") String authentication) {
        String userName = TokenUtil.getUserInfoFromToken(authentication);
        User user = userService.getUserByUserName(userName);
        UserResponseVM model = modelMapper.map(user, UserResponseVM.class);
        return RestResponse.ok(model);
    }

    /**
     * 功能描述 注册接口
     *
     * @param model
     *
     * @return com.zhu.base.http.RestResponse<java.lang.Object>
     *
     * @author JoyBoyZH
     * @date 2022/5/30
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/register")
    @ApiOperation("注册接口说明")
    public RestResponse<Object> register(@RequestBody UserRegisterVM model) {
        User user = modelMapper.map(model, User.class);
        user.setRole(UserRoleEnum.USER);
        user.setImageUrl("https://gitee.com/zhu-wang/cloud-library/raw/master/static/pic/1655815025792_10f037e5-40cd-4dae-aabd-17239b9aca64.png");
        User user1 = userService.getUserByUserName(user.getUserName());
        if (user1 != null) {
            return RestResponse.failure("信息重复");
        } else {
            userService.saveUser(user);
            return RestResponse.ok("注册成功");
        }
    }

    @PostMapping(value = "/updateUser")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("修改信息接口说明")
    public RestResponse<Object> updateUser(@RequestBody UserUpdateVM model, @RequestHeader("authentication") String authentication) {
        String userName = TokenUtil.getUserInfoFromToken(authentication);
        User user = userService.getUserByUserName(userName);
        User userRepeat = userService.getUserByPhone(model.getPhone());
        if (userRepeat == null || userRepeat.getUserId().equals(user.getUserId())) {
            /**
             * 前端设置默认值，不能为空
             */
            inventoryRecordService.updateRealName(user.getRealName(),model.getRealName());
            user.setRealName(model.getRealName());
            user.setPhone(model.getPhone());
            user.setAddress(model.getAddress());
            userService.updateUserById(user);
            return RestResponse.ok("修改成功");
        } else {
            return RestResponse.failure("电话重复,修改失败");
        }
    }

    @PostMapping(value = "/updatePassword")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("修改密码接口说明")
    public RestResponse<Object> updatePassword(@RequestBody UserUpdatePasswordVM model) {
        User user = userService.getUserByUserName(model.getUserName());
        user.setPassword(model.getNewPassword());
        if (userService.updateUserById(user)) {
            return RestResponse.ok("修改成功");
        } else {
            return RestResponse.failure("修改失败");
        }
    }

    @GetMapping(value = "/getAllProvinces")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("获取全国省份接口说明")
    public RestResponse<Object> getAllProvinces() {
        List<Provinces> list = provincesService.getAllProvinces();
        if (list != null) {
            return RestResponse.ok(list);
        } else {
            return RestResponse.failure("查询失败！");
        }
    }

    @PostMapping(value = "/getCities/{provincesId}")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("获取省对应城市接口说明")
    public RestResponse<Object> getCities(@ApiParam("省编号") @PathVariable String provincesId) {
        List<Cities> list = citiesService.getCities(provincesId);
        if (list != null) {
            return RestResponse.ok(list);
        } else {
            return RestResponse.failure("查询失败！");
        }
    }

    @PostMapping(value = "/getAreas/{citiesId}")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("获取市对应县接口说明")
    public RestResponse<Object> getAreas(@ApiParam("市编号") @PathVariable String citiesId) {
        List<Areas> list = areasService.getAreas(citiesId);
        if (list != null) {
            return RestResponse.ok(list);
        } else {
            return RestResponse.failure("查询失败！");
        }
    }
}

